package the.one.weather.ui.fragment;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.OnLifecycleEvent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import com.qmuiteam.qmui.layout.QMUIFrameLayout;
import com.qmuiteam.qmui.widget.QMUIObservableScrollView;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import butterknife.BindView;
import the.one.base.base.fragment.BaseFragment;
import the.one.base.base.presenter.BasePresenter;
import the.one.base.constant.DataConstant;
import the.one.base.widge.WWPullRefreshLayout;
import the.one.weather.R;
import the.one.weather.event.ScrollEvent;
import the.one.weather.model.Weather;
import the.one.weather.presenter.WeatherPresenter;
import the.one.weather.ui.activity.Weather2Activity;
import the.one.weather.ui.adapter.WeatherFutureAdapter;
import the.one.weather.ui.adapter.WeatherTipsAdapter;
import the.one.weather.util.LitePalUtil;
import the.one.weather.view.WeatherView;


//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛

/**
 * @author The one
 * @date 2019/5/7 0007
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
public class WeatherItemFragment extends BaseFragment implements WeatherView, QMUIPullRefreshLayout.OnPullListener, QMUIObservableScrollView.OnScrollChangedListener {

    public static WeatherItemFragment newInstance(Weather weather) {
        WeatherItemFragment fragment = new WeatherItemFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(DataConstant.DATA, weather);
        fragment.setArguments(bundle);
        return fragment;
    }

    @BindView(R.id.scrollView)
    QMUIObservableScrollView mScrollView;
    @BindView(R.id.pullLayout)
    WWPullRefreshLayout mPullRefreshLayout;
    @BindView(R.id.recycle_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.recycle_view_tips)
    RecyclerView mRecyclerViewTips;
    @BindView(R.id.top_layout)
    RelativeLayout mTopLayout;
    @BindView(R.id.parent)
    QMUIFrameLayout mParent;
    @BindView(R.id.tv_temperature)
    AppCompatTextView tvTemperature;
    @BindView(R.id.tv_unit)
    AppCompatTextView tvUnit;

    private Weather mWeather;
    private WeatherPresenter presenter;
    private WeatherFutureAdapter mFutureAdapter;
    private WeatherTipsAdapter mTipsAdapter;
//    private WeatherFragment mWeatherFragment;
    private Weather2Activity mWeather2Activity;
    private int mTopHeight;
    private int mTempHeight;
    private int mShowTitleHeight;

    @Override
    protected void initView(View rootView) {
        assert getArguments() != null;
        mWeather = getArguments().getParcelable(DataConstant.DATA);
//        mWeatherFragment = (WeatherFragment) getParentFragment();
        mWeather2Activity = (Weather2Activity) getActivity();
        ViewTreeObserver vto = mTopLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mTopHeight = mTopLayout.getHeight();
                mTempHeight = tvTemperature.getHeight();
                mShowTitleHeight = mTopHeight - mTempHeight;
                mScrollView.addOnScrollChangedListener(WeatherItemFragment.this);
            }
        });
        mPullRefreshLayout.setOnPullListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mFutureAdapter = new WeatherFutureAdapter();
        mRecyclerView.setAdapter(mFutureAdapter);

        mRecyclerViewTips.setLayoutManager(new GridLayoutManager(_mActivity, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mTipsAdapter = new WeatherTipsAdapter();
        mRecyclerViewTips.setAdapter(mTipsAdapter);
        Log.e(TAG, "initView: "+mWeather.getCurrentCity() );
        onSuccess(mWeather);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onLazyResume() {
        if (!isIndexFragment && mIsFirstLayInit) {
            mIsFirstLayInit = false;
            onRefresh();
        }
        playWeatherVideo();
    }

    @Override
    public void onSuccess(Weather weather) {
        if (null == weather || null == weather.getFutures() || TextUtils.isEmpty(weather.getWeather())) return;
        mWeather = weather;
        tvTemperature.setText(weather.getTemperature());
        int color = getColorr(weather.getWeatherColor());
        tvTemperature.setTextColor(color);
        tvUnit.setTextColor(color);
        mFutureAdapter.setNewData(weather.getFutures());
        mTipsAdapter.setNewData(weather.getTips());
        mTipsAdapter.addData(weather.getTips());
        mTipsAdapter.addData(weather.getTips());

        showView(mPullRefreshLayout);
        mPullRefreshLayout.finishRefresh();
        playWeatherVideo();
        LitePalUtil.saveCityWeather(weather);
    }

    private void playWeatherVideo() {
        if (null != mWeather2Activity && null != mWeather) {
            mWeather2Activity.playWeatherVideo(mWeather);
        }
    }

    @Override
    public void onMoveTarget(int offset) {
        if (offset < 200)
            updateParentBgAlpha((float) offset / 200);
    }

    @Override
    public void onMoveRefreshView(int offset) {
    }

    @Override
    public void onRefresh() {
        presenter.getWeatherInfo(mWeather);
    }

    @Override
    public void onScrollChanged(QMUIObservableScrollView scrollView, int x, int y, int oldl, int oldt) {
        //第一个参数为变化后的X轴位置
        //第二个参数为变化后的Y轴的位置
        //第三个参数为原先的X轴的位置
        //第四个参数为原先的Y轴的位置
        int offset = scrollView.getScrollOffset();
        int p = offset / mShowTitleHeight;
        if (p > 1) p = 1;
        mWeather2Activity.onScroll(new ScrollEvent(x, y, p * 255));

        float percent ;
        if (offset <= mTopHeight) {
            percent = (float) offset / mTopHeight;
        } else {
            percent = 0.9f;
        }
        if (percent > 0.9) {
            percent = 0.9f;
        }
        updateParentBgAlpha(percent);
    }

    private void updateParentBgAlpha(float percent) {
        float alpha = (255 * percent);
        mParent.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
    }

    public void scrollTo(ScrollEvent scrollEvent) {
        if (null != mScrollView && null != scrollEvent) {
            mScrollView.scrollTo(scrollEvent.getX(), scrollEvent.getY());
        }
    }

    public Weather getWeather() {
        return mWeather;
    }

    @Override
    public void onResume() {
        super.onResume();
        mScrollView.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        ScrollEvent scrollEvent = mWeather2Activity.getScrollEvent();
                        scrollTo(scrollEvent);
                    }
                });
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_weather;
    }

    @Override
    protected boolean showTitleBar() {
        return false;
    }

    @Override
    public BasePresenter getPresenter() {
        return presenter = new WeatherPresenter();
    }
}
